package com.xxxx.server.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.xxxx.server.mapper.EmployeeMapper;


import com.xxxx.server.page.EmployeePage;
import com.xxxx.server.page.LocalDateTimeConverter;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-08-17
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPositionService positionService;

    @Autowired
    private INationService nationService;

    @Autowired
    private IJoblevelService joblevelService;

    @Autowired
    private IPoliticsStatusService politicsStatusService;

    @Autowired
    private IDepartmentService departmentService;




    //进入员工页面 展示员工数据信息
    @RequestMapping(value = "/findAll/{currentPage}&{size}",method = RequestMethod.POST)
    //@GetMapping("/")
    @ApiOperation(value = "获取所有员工信息列表")
    public Object findAll(@PathVariable("currentPage") Integer currentPage,@PathVariable("size")Integer size){
        //获取前台发送过来的数据
        IPage<Employee> page = new Page<>(currentPage, size);
        return employeeService.findList(page);
    }

    //根据员工姓名查询员工对象
    //@GetMapping("/getAllEmployeeByName")
    @GetMapping(value = "/{name}")
    @ApiOperation(value = "根据员工姓名获取指定员工信息列表")
    public Employee getAllEmployeeByName(String name){
         return employeeService.getEmployeeByName(name);
    }

    //多条件查询员工信息
    @GetMapping("/")
    @ApiOperation(value = "多条件查询员工对象")
    public Map<String, Object> selectEmployeeList (EmployeePage employeePage){
        return employeeService.queryByParamsForTable(employeePage);
    }


    //添加员工信息
    @PostMapping("/add")
    @ApiOperation(value = "添加员工信息")
    public RespBean addEmployee(@RequestBody Employee employee){
        //service 层做校验
        if(employeeService.save(employee)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！！！");
    }

    //更新员工信息
    @PutMapping("/update")
    @ApiOperation(value = "更新员工信息")
    public RespBean updateEmployee(@RequestBody Employee employee){
        if(employeeService.updateById(employee)){
            return RespBean.success("更新成功！！！");
        }
        return RespBean.error("更新失败！！");
    }

    //删除员工信息
    @DeleteMapping("/")
    @ApiOperation(value = "删除员工信息")
    public RespBean deleteEmployee(@RequestBody Integer id){
        if(employeeService.removeById(id)){
            return RespBean.success("删除成功！！！");
        }
        return RespBean.error("删除失败！！");
    }


    @GetMapping("/positions")
    @ApiOperation(value = "查询出所有的职位")
    public List<Position> positions(){
        return positionService.list();
    }
    @GetMapping("/nations")
    @ApiOperation(value = "查询出所有的名族")
    public List<Nation> nations(){
        return nationService.list();
    }
    @GetMapping("/joblevels")
    @ApiOperation(value = "查询出所有的职称")
    public List<Joblevel> joblevels(){
        return joblevelService.list();
    }
    @GetMapping("/politicsstatus")
    @ApiOperation(value = "查询出所有的政治面貌")
    public List<PoliticsStatus> politicsStatuses(){
        return politicsStatusService.list();
    }

   /* @GetMapping("/allDeps")
    @ApiOperation(value = "查询出所有的所属部门")
    public  List<Department>  departments(){
        return departmentService.list();
    }
*/

    @GetMapping("/export")
    @ApiOperation(value = "导出数据")
    public void downLoadData(HttpServletResponse response)  {
        List< Employee > exports = employeeService.findAllEmployee();
        try {
            ServletOutputStream out = response.getOutputStream();
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX,true);
            String fileName = new String("Student信息表".getBytes("utf-8"),"utf-8");
            Sheet sheet = new Sheet(1,0,Employee.class);
            sheet.setSheetName("sheet1");
            response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            writer.write(exports,sheet);
            writer.finish();
            out.flush();
        }catch (Exception e) {
            e.printStackTrace();

        }
    }

    @GetMapping("/import")
    @ApiOperation(value = "导入数据")
    public void importData(){


    }

}
